package com.yun.flowablestudy.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author chengyun
 * @version 1.0
 * @date 2022/4/27 15:42
 */
@Controller
@RequestMapping(value = "cy")
@Slf4j
public class ApproveController {
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private HistoryService historyService;
    /**
     * 部署流程定义
     */
/*    @RequestMapping("/definition")
    @ResponseBody
    public String deploymentFlowDefinition(){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("flowable/approve_cy.bpmn20.xml")
                .name("请假流程部署")
                .deploy();
        return "部署成功";
    }*/
    @GetMapping("add")
    @ResponseBody
    public String add(String userId, String days, String reason){
        HashMap<String, Object> map = new HashMap<>();
        map.put("staffId", userId);
        map.put("noDays", days);
        map.put("reason", reason);
        // apply xml文件中<process>标签的id
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leave_key", map);
        log.info(JSON.toJSONString(processInstance.toString()));
        return "流程ID:"+ processInstance.getId();
    }
    @RequestMapping("/query/task")
    @ResponseBody
    public String queryTask(){
        TaskService taskService = processEngine.getTaskService();
        Task task = taskService.createTaskQuery().taskAssignee("张三").singleResult();
        Map<String, Object> processVariables = taskService.getVariables(task.getId());

        List<Task> tasks = taskService.createTaskQuery().taskAssignee("张三").orderByTaskCreateTime().desc().list();
        tasks.stream().forEach(t1->log.info(JSON.toJSONString(taskService.getVariables(t1.getId()))));
        return task.getId();
    }
    @RequestMapping("/complete/task")
    @ResponseBody
    public String completeTask(String taskId){
        TaskService taskService = processEngine.getTaskService();
       // taskService.complete(taskId);

        List<Task> t = taskService.createTaskQuery().list();
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();

        if (task == null) {
            throw new RuntimeException("流程不存在");
        }
        //通过审核
        HashMap<String, Object> map = new HashMap<>();
        map.put("outcome", "通过");
        taskService.complete(taskId, map);
        return "成功";
    }


}
